﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Services.Tokens;

namespace HyperCrunch.Druid
{
    public static class Globals
    {
        public const string DRUID_CONFIG_FILE = "druid.config";

        public const string ID_ATTRIBUTE = "id";
        public const string IGNORE_IF_ATTRIBUTE = "ignoreIf";
        public const string ALLOW_OVERRIDE_ATTRIBUTE = "allowOverride";
        public const string ORDER_ATTRIBUTE = "order";
        public const string CONTINUE_AFTER_MATCH_ATTRIBUTE = "continueAfterMatch";

        public const string MATCH_TYPE_ATTRIBUTE = "matchType";
        public const string MATCH_FIELD_ATTRIBUTE = "matchField";
        public const string MATCH_VALUES_ATTRIBUTE = "matchValues";
        public const string MATCH_CONDITION_ATTRIBUTE = "matchCondition";

        public const string DEBUG_ATTRIBUTE = "debug";
        public const string DATA_STYLESHEET_URL_ATTRIBUTE = "data-stylesheet-url";
        public const string DATA_SCRIPT_URL_ATTRIBUTE = "data-script-url";
        public const string DATA_SKIN_CONTROL_ATTRIBUTE = "data-skin-control";
        public const string DATA_CONTAINER_CONTROL_ATTRIBUTE = "data-container-control";
        public const string DATA_THEME_ATTRIBUTE = "data-theme";
        public const string DATA_CONTENT_RENDERER_ATTRIBUTE = "data-content-renderer";
        public const string DATA_CONTENT_MODIFIER_ATTRIBUTE = "data-content-modifier";
        public const string DATA_REDIRECT_URL_ATTRIBUTE = "data-redirect-url";
        public const string JSON_SETTING_NAME_ATTRIBUTE = "json-setting-name";
        public const string JSON_SETTING_VALUE_ATTRIBUTE = "json-setting-value";

        public const string CONTEXT_THEME = "page_theme";
        public const string SITE_RETURN_URL_COOKIE_NAME = "site_return_url";

        public static string ResolveUrl(UIContext uiContext, string url)
        {
            if (url.StartsWith("~"))
                url = url.Replace("~", uiContext.Context.Request.ApplicationPath).Replace("//", "/");

            url = ReplaceTokens(url, uiContext);

            return url;
        }

        public static string ReplaceTokens(string s, UIContext uiContext)
        {
            if ((s.IndexOf("[") > -1) && (s.IndexOf("]") > -1))
            {
                TokenReplace tr = new TokenReplace();
                if (uiContext.Context.Request.IsAuthenticated)
                    tr.User = uiContext.User;
                tr.PortalSettings = uiContext.PortalSettings;
                return tr.ReplaceEnvironmentTokens(s);
            }
            else
                return s;
        }
    }

    public enum RuleProcessingResultType
    {
        RedirectResponse,
        RenderCustomResponse,
        ModifyStandardResponse

    }

    [Serializable()]
    public enum IgnoreConditionType
    {
        Never,
        Authenticated,
        Administrator,
        Superuser
    }

    [Serializable()]
    public enum AllowOverrideType
    {
        Never,
        Always
    }

    [Serializable()]
    public enum ContinueAfterMatchType
    {
        Never,
        Always
    }

    [Serializable()]
    public enum MatchConditionType
    {
        Equal,
        NotEqual,
        LessThan,
        LessThanEqual,
        GreaterThan,
        GreaterThanEqual,
        InList,
        NotInList,
        StartsWith,
        EndsWith,
        Contains,
        Exists
    }

    public enum LogType
    {
        Match,
        NoMatch,
        Info,
        Error
    }

}
